#include "base64.h"

static const char chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

static const int8_t tables[] = {
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -2, -2, -1, -2, -2,
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
    -1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63,
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -2, -2, -2,
    -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, -2,
    -2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2,
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2
};

static bool useBase64 = false;

String base64_encode(const char* bytes, unsigned int len) {
    if (useBase64 == false) {
        return String(bytes).substring(0, len);
    }
    int num = 0, bin = 0, i;
    String result;
    const unsigned char* curr = (const unsigned char*)bytes;
    while (len > 2) {
        result += chars[curr[0] >> 2];
        result += chars[((curr[0] & 0x03) << 4) + (curr[1] >> 4)];
        result += chars[((curr[1] & 0x0f) << 2) + (curr[2] >> 6)];
        result += chars[curr[2] & 0x3f];
        curr += 3;
        len -= 3;
    }
    if (len > 0) {
        result += chars[curr[0] >> 2];
        if (len % 3 == 1) {
            result += chars[(curr[0] & 0x03) << 4];
            result += "==";
        } else if (len % 3 == 2) {
            result += chars[((curr[0] & 0x03) << 4) + (curr[1] >> 4)];
            result += chars[(curr[1] & 0x0f) << 2];
            result += "=";
        }
    }
    return result;
}

String base64_decode(const char* bytes, unsigned int len) {
    if (useBase64 == false) {
        return String(bytes).substring(0, len);
    }
    int bin = 0, i = 0, pos = 0;
    String result;
    const char* curr = bytes;
    char ch;
    while ((ch = *curr++) != '\0' && len-- > 0) {
        if (ch == '=') {
            if (*curr != '=' && (i % 4) == 1) {
                return "";
            }
            continue;
        }
        ch = (char)tables[ch];
        if (ch < 0) {
            continue;
        }
        switch (i % 4) {
            case 0: {
                bin = ch << 2;
            } break;
            case 1: {
                bin |= ch >> 4;
                result += bin;
                bin = (ch & 0x0f) << 4;
            } break;
            case 2: {
                bin |= ch >> 2;
                result += bin;
                bin = (ch & 0x03) << 6;
            } break;
            case 3: {
                bin |= ch;
                result += bin;
            } break;
        }
        i++;
    }
    return result;
}